package com.wxw.test;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LinearGradientPaint;
import java.awt.MultipleGradientPaint.CycleMethod;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Arc2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import java.util.Calendar;

import javax.imageio.ImageIO;
import javax.swing.JPanel;


public class MyPanel extends JPanel
{

    public MyPanel()
    {
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        int width = getWidth();
        int height = getHeight();
        Graphics2D g2d = (Graphics2D) g;

        // 平滑绘制 （ 反锯齿 )
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setPaint(Color.white);
        g2d.fillRect(0, 0, width, height);

        // 10个顶点的坐标
        // 注意：这里是以中心为原点、x轴向右、y轴向上的坐标系，标准圆半径为1
        Point2D.Double pA = new Point2D.Double(0, 1);
        Point2D.Double pB = new Point2D.Double(-0.95106,0.30902);
        Point2D.Double pC = new Point2D.Double(-0.58779,-0.80902);
        Point2D.Double pD = new Point2D.Double(0.58779,-0.80902);
        Point2D.Double pE = new Point2D.Double(0.95106,0.30902);
        Point2D.Double pF = new Point2D.Double(0,-0.38197);
        Point2D.Double pG = new Point2D.Double(0.36328,-0.11804);
        Point2D.Double pH = new Point2D.Double(0.22452,0.30902);
        Point2D.Double pI = new Point2D.Double(-0.22452,0.30902);
        Point2D.Double pJ = new Point2D.Double(-0.36328,-0.11804);
        Point2D.Double[] points = { pA, pI, pB, pJ, pC, pF, pD, pG, pE, pH };

        // 坐标转换 ( 指定目标正方形，在里面画一个五角星 )
        Rectangle rect = new Rectangle(30, 30, 200,200);
        int radius_x = rect.width / 2;
        int radius_y = rect.width / 2;
        for(Point2D.Double point : points)
        {
            point.x = rect.getCenterX() + radius_x * point.x ;
            point.y = rect.getCenterY() - radius_y * point.y ; // 坐标反转
        }

        // 画五角星
        Path2D outline = new Path2D.Double();
        outline.moveTo(points[0].x, points[0].y);
        for(int i=1; i<points.length; i++)
        {
            outline.lineTo(points[i].x, points[i].y);
        }
        outline.closePath();

        Color fillColor = Color.YELLOW;
        g2d.setPaint(fillColor);
        g2d.fill(outline);

        // 画圆
        Shape circle = new Ellipse2D.Double(rect.x, rect.y, rect.width, rect.height);
        g2d.draw(circle);


    }


}
